package com.example.zuoye_jiajianfuwei;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import com.example.zuoye_jiajianfuwei.R;

public class MainActivity extends AppCompatActivity {

    private int counter = 0;
    private TextView counterText;
    private Button decrementButton, incrementButton, resetButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化
        counterText = findViewById(R.id.counterText);
        decrementButton = findViewById(R.id.decrementButton);
        incrementButton = findViewById(R.id.incrementButton);
        resetButton = findViewById(R.id.resetButton);

        decrementButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                counter--;
                updateCounterText();
            }
        });

        incrementButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                counter++;
                updateCounterText();
            }
        });

        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                counter = 0;
                updateCounterText();
                Toast.makeText(MainActivity.this, "已重置", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void updateCounterText() {
        counterText.setText(String.valueOf(counter));
    }
}